/**
 * *******************************      首页图片管理   *************************
 */
var removeImageId = '';
/**
 * 获取所有图片信息
 */
function getHomeImage() {
	$.ajax({
		url : '../home/getAllHomePageImages',
		type : 'GET',
		dataType : 'json',
		contentType : 'application/json',
		success : function(data) {
			if(data.success) {
				renderHomeImageForManagement(data.resultList, 'homeImageContent');
			}
		}
	})
}

/**
 * 渲染管理首页图片页面
 * @param data
 * @param contentName
 */
function renderHomeImageForManagement(data, contentName) {
	var len = data.length;
	$('#' + contentName).empty();
	for(var i = 0;i < len; i= i+2) {
		var str = '';
		if(i + 1 >= len) {
			str = assembledHomeImage(data[i]);
		}else{
			str = assembledHomeImage(data[i], data[i+1]);
		}
		//console.log(str);
		$('#' + contentName).append(str);
	}
}

/**
 * 拼装字符串
 * @returns {String}
 */
function assembledHomeImage() {
	var args = arguments;
	var len = args.length;
	var str = '<div class="row-fluid padd-bottom" style="margin-top: 20px;text-align: center;">';
	for(var i = 0 ; i<len;i++) {
		str += '<div class="span5 thumbnail" style="margin-left: 60px;"><a href="#"><img alt="'+args[i].documentDesc +
		'" style="width: 360px; height: 180px;" src="../../'+args[i].documentUrl +
		'"></a><div style="text-align: center; margin-top: 10px;margin-bottom: 10px;"><button type="button" onclick="showDeleteHomeModal('+ args[i].documentId +
		');" class="btn btn-danger">删除宣传图片</button></div></div>';
	}
	str += '</div>';
	return str;
}





/**
 * 首页图片上传
 */
function uploadHomeImage() {
	$('#homeImage').fileupload({
		 autoUpload: true,//自动上传
		 dataType : 'json',
		 type: 'POST',
		 url : '../upload/uploadHomePageImage',
		 success : function(data) {
			 if(data.success){
					//上传成功
				toastr.success(data.msg);
			}else{
				toastr.error(data.msg);
			}
			 //更新
			 getHomeImage();
		 }
	})
}

/**
 * 打开删除首页模态框，确认用户确定删除指定首页展示图片
 * @param documentId
 */
function showDeleteHomeModal(documentId) {
	removeImageId = documentId;
	
	showModal('../modalTemplates/removeHomeImage.html');
}


/**
 * 删除首页展现图片
 */
function deleteHomeImage() {
	$.ajax({
		url : '../home/deleteHomeImage',
		type : 'DELETE',
		dataType : 'json',
		contentType : 'application/json',
		data : JSON.stringify({
			documentId : removeImageId
		}),
		success : function(data) {
			if(data.success) {
				toastr.success(data.msg);
			}else{
				toastr.error(data.msg);
			}
			
			$("#modalModule").modal('hide');
			getHomeImage();
		}
	})
}